Filter Data

Multiple Filters that Dynamically Update Each Other

Description
This customization shows how to implement multiple filters that dynamically update each other.
Variables
First Filter Control
Select a filter control whose items are populated from a number or character type field in the database
Second Filter Control
Select another filter control whose items are populated from a number or character type field in the database
Parent Table Control
Select the parent table control where the filter exist.
Table
Select the database table associated with the Parent Table Control
First Filter Field
Select the field associated with first filtert
Second Filter Field
Select the field associated with second filter
Applies to
TableControl class
Code
 
/// 
/// This method sets the AutoPostBack property of the field that triggers a change.
/// 
/// The object that raised the init event.
/// The object that contains the event data of the init event.
private void MultipleDropdown_MyInit(object sender, System.EventArgs e) 
{   
	// AutoPostBack sets or retrieves a value that indicates whether or not the control
	// posts back to the server each time a user interacts with the control. 
	// if change in second filter updates the first then set
	// AutoPostback of the second filter to true and implement 
	${First Filter Control}.AutoPostBack = true;
	${Second Filter Control}.AutoPostBack = true;
    

    // Define selected index changed event handlers
    // for first filter.
    this.${First Filter Control}.SelectedIndexChanged += 
    new EventHandler(${First Filter Control}_SelectedIndexChanged);
    
    ${Second Filter Control}.Enabled = false;
}
 
Applies to
TableControl class
Code
 
/// 
/// This method is called when selected index changes in ${First Filter Control}.
/// 
protected override void ${First Filter Control}_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    ${Second Filter Control}.Enabled = true;   

    // ${Second Filter Control} Filter will display 100 items.
    // You can set the number of items displayed in the Filter.
    this.Populate${Second Filter Control}DropDown(100);    
} 
     
Applies to
TableControl class
Code
 
/// 
/// Override this method to filter the  ${Second Filter Control}DropDownList
/// based on the value selected for the ${First Filter Control}DropDownList
/// 
protected void Populate${Second Filter Control}DropDown(int maxItems)
{
    // Set up the WHERE clause.
    // Create the WHERE clause to filter the second filter based on the 
    // selected value in the first filter.
    WhereClause wc = new WhereClause();
    string selectedValue = ${First Filter Control}.SelectedValue;  
    string selectedText = ${First Filter Control}.SelectedItem.Text;   
    wc.iAND(${${Table}ClassName}.${First Filter Field}, BaseFilter.ComparisonOperator.EqualsTo, selectedValue);

    // Clear the contents of second filter.
    this.${Second Filter Control}.Items.Clear();    
    
    // Add "Please Select" string to second filter.   
    this.${Second Filter Control}.Items.Insert(0, new ListItem(Page.GetResourceValue("Txt:PleaseSelect", "${Application Name}"), "--PLEASE_SELECT--"));                      
    
    if(BaseClasses.Utils.StringUtils.InvariantUCase(selectedText).Equals(BaseClasses.Utils.StringUtils.InvariantUCase(Page.GetResourceValue("Txt:PleaseSelect", "${Application Name}"))))
    {
        // if "Please Select" string is selected for first filter,
        // then do not continue populating the second filter.
        return;
    }    
    
    // Get the records using the created where clause.
    foreach ( ${${Table}RecordClassName} itemValue in ${${Table}ClassName}.GetRecords(wc, null, 0, maxItems))
    {
        if(itemValue.${Second Filter Field}Specified)
        {
            // In each record, obtain the value of Second Filter Field if value exists,
            // create an item for it and add it to the list.
            string cvalue = itemValue.${Second Filter Field}.ToString();
            string fvalue  = itemValue.Format(${${Table}ClassName}.${Second Filter Field});
            ListItem item  = new ListItem(fvalue, cvalue);
            if (! this.${Second Filter Control}.Items.Contains(item))
            {
                this.${Second Filter Control}.Items.Add(item);        
            }
        }        
    }                    

    // Select "Please Select" string in the second filter.
    this.${Second Filter Control}.SelectedIndex = 0;
}
    
Applies to
CSharpTableControlConstructor class
Code
 
    // The following line will be inserted inside the
    // constructor for page class.
    this.Init += new EventHandler(MultipleDropdown_MyInit);
     

Terms of Service Privacy Statement